package cn.mogugo.joke.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.comm.CC;
import cn.mogugo.comm.PageSupport;
import cn.mogugo.joke.action.form.JokeCategoryForm;
import cn.mogugo.joke.model.JokeCategory;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.util.tools.NumberTools;
import cn.mogugo.util.tools.Page;

public class JokeCategoryAction extends BaseAction{
	/**
	 * 默认方法
	 */
	public ActionForward unspecified(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return list(mapping,form,request,response);
	}
	
	
	public ActionForward list(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		List list = this.jokeCategoryManager.getAll();
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward create(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return mapping.findForward("edit");
	}
	
	
	public ActionForward edit(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeCategory jc = (JokeCategory)this.baseDao.get(JokeCategory.class, Integer.valueOf(id));
		JokeCategoryForm JokeCategoryForm = (JokeCategoryForm)form;
		BeanUtils.copyProperties(JokeCategoryForm ,jc);
		

		return mapping.findForward("edit");
	}
	
	
	public ActionForward saveOrUpdate(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		JokeCategoryForm JokeCategoryForm = (JokeCategoryForm)form;
		JokeCategory jc;
		if(StringUtils.isNotBlank(JokeCategoryForm.getId())){
			jc = (JokeCategory)this.baseDao.get(JokeCategory.class, Integer.valueOf(JokeCategoryForm.getId()));
		}else{
			jc = new JokeCategory();
		}
		BeanUtils.copyProperties(jc , JokeCategoryForm);
		if(jc.getId() == 0){
			jc.setId(null);
		}
		this.baseDao.saveOrUpdateLog(jc);
		
		return mapping.findForward("tolist");
	}
	
	
	
	public ActionForward delete(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		this.baseDao.deleteObjectByIdLog(JokeCategory.class, Integer.valueOf(id));
		return mapping.findForward("tolist");
	}
	
	
	
	public ActionForward saveStat(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		List list = this.jokeCategoryManager.getAll();
		for(int i = 0 ; i < list.size() ; i ++ ){
			JokeCategory jc = (JokeCategory)list.get(i);
			Integer joNumber = this.jokeObjManager.getNumberByCategoryId(jc.getId());
			jc.setJoke_number(joNumber);
			this.baseDao.saveOrUpdate(jc);
		}
		return mapping.findForward("tolist");
	}

	public ActionForward editUpdateToAnother(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeCategory curJc = (JokeCategory)this.baseDao.get(JokeCategory.class, Integer.valueOf(id));
		
		List listCategory = this.jokeCategoryManager.getAllWithOutCur(Integer.valueOf(id));
		request.setAttribute("list", listCategory);
		request.setAttribute("curJc", curJc);
		
		return mapping.findForward("editUpdateToAnother");
	}
	
	
	
	public ActionForward updateToAnother(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		JokeCategoryForm jokeCategoryForm = (JokeCategoryForm)form;

		this.jokeObjManager.updateListToAnotherCategory(Integer.valueOf(jokeCategoryForm.getId()) , jokeCategoryForm.getTo_id());
		
		return mapping.findForward("tolist");
	}
	
	
	
	public ActionForward listJokeObj(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		
		String id = request.getParameter("id");
		JokeCategory curJc = (JokeCategory)this.baseDao.get(JokeCategory.class, Integer.valueOf(id));
		
		Map map = new HashMap();
		map.put("categoryId", Integer.valueOf(id));
		PageSupport psObj = this.jokeObjManager.getPage(map, pageNo, CC.PAGE_SIZE, "new");
		
		request.setAttribute("curJc", curJc);
		
		request.setAttribute("page", psObj.getItems());
    	request.setAttribute("pageStr", Page.getInstance().createPageStr(request, psObj));
    	
		return mapping.findForward("listJokeObj");
	}
}
